package org.zty.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static Calendar initCurrentDate()
	{
		Date date = new Date(System.currentTimeMillis());
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}
	/**
	 * 获取当前的年
	 * @return
	 */
	public static int getCurrentYear()
	{
		Calendar c = initCurrentDate();
		return c.get(Calendar.YEAR);
	}
	/**
	 * 获取当前的月份
	 * @return
	 */
	public static int getCurrentMonth(){
		return initCurrentDate().get(Calendar.MONTH)+1;
	}
	/**
	 * 获取当前日期
	 * @return
	 */
	public static int getCurrentDay(){
		return initCurrentDate().get(Calendar.DAY_OF_MONTH);
	}
	/**
	 * 获取当前小时
	 * @return
	 */
	public static int getCurrentHour(){
		return initCurrentDate().get(Calendar.HOUR_OF_DAY);
	}
	/**
	 * 获取当前分钟
	 * @return
	 */
	public static int getCurrentMinute(){
		return initCurrentDate().get(Calendar.MINUTE);
	}
	/**
	 * 获取当前秒
	 * @return
	 */
	public static int getCurrentSecond(){
		return initCurrentDate().get(Calendar.SECOND);
	}
	/**
	 * 获取当前日志
	 * @param format( yyyy-MM-dd /yyyy-MM-dd HH:mm:ss等）
	 * @return
	 */
	public static String getCurrentDate(String format){
		SimpleDateFormat parse = new SimpleDateFormat(format);
		return parse.format(new java.util.Date());
	}
	/**
	 * 将日期字符串转为java.util.Date对象
	 * @param str
	 * @param parsePatterns
	 * @return
	 * @throws ParseException
	 */
	public static java.util.Date parseDate(String str, String parsePatterns) throws ParseException {
        if (str == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }
        
        SimpleDateFormat parser = null;
        ParsePosition pos = new ParsePosition(0);
                parser = new SimpleDateFormat(parsePatterns);
            pos.setIndex(0);
            java.util.Date date = parser.parse(str, pos);
            if (date != null && pos.getIndex() == str.length()) {
                return date;
            }
        throw new ParseException("Unable to parse the date: " + str, -1);
    }
	/**
	 * 将日期对象转为字符串
	 * @param date
	 * @param par
	 * @return
	 */
	public static String dateToString(java.util.Date date,String par)
	{
		if(date==null){
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(par);
		return sdf.format(date);
	}
	/**
	 * 将日期字符串转换为长整型
	 * @param date 格式(yyyy-MM-dd)
	 * @return
	 */
	public static long strToDateLong(String date) {
		long l = 0L;
		try {
			Calendar ca = Calendar.getInstance();
			ca.setTime(java.sql.Date.valueOf(date));
			l=ca.getTimeInMillis();
		} catch (Exception e) {
			new ZException("日期字符串转长整形错误",e);
		}
		return l;
	}
	/**
	 * 将long数字转为日期字符串
	 * @param format
	 * @param time
	 * @return
	 */
	public static String longToStrDate(String format,long time){
		DateFormat df = new SimpleDateFormat(format);
		return df.format(new Date(time));
	}
	/**
	 * 将字符串转为日期对象
	 * @param str
	 * @param parsePatterns
	 * @return
	 * @throws ParseException
	 */
	public static java.util.Date strToDate(String str, String parsePatterns) throws ParseException {
		return parseDate(str,parsePatterns);
	}
	/** 
     * 计算两个日期之间相差的天数 
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
	 * @throws ParseException 
     */  
    public static int daysBetween(Date smdate,Date bdate) throws ParseException  
    {  
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    	smdate=sdf.parse(sdf.format(smdate));
    	bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();  
        cal.setTime(smdate);  
        long time1 = cal.getTimeInMillis();               
        cal.setTime(bdate);  
        long time2 = cal.getTimeInMillis();       
        long between_days=(time2-time1)/(1000*3600*24);
          
       return Integer.parseInt(String.valueOf(between_days));         
    }  
    
/**
*字符串的日期格式的计算
*/
    public static int daysBetween(String smdate,String bdate) throws ParseException{
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();  
        cal.setTime(sdf.parse(smdate));  
        long time1 = cal.getTimeInMillis();               
        cal.setTime(sdf.parse(bdate));  
        long time2 = cal.getTimeInMillis();       
        long between_days=(time2-time1)/(1000*3600*24);
          
       return Integer.parseInt(String.valueOf(between_days));   
    }
}
